package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.OrderCouponListAdapter;
import com.longcai.piaojia.conn.OrderCouponListGet;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/5.
 */
public class OrderCouponActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(R.id.order_coupon_lv) private ListView order_coupon_lv;
    @BoundView(R.id.order_coupon_tv) private TextView order_coupon_tv;
    private List<OrderCouponListGet.OrderCouponEntity> lists = new ArrayList<>();
    private OrderCouponListAdapter orderCouponListAdapter;

    private Map<Integer, Integer> idsMap = new HashMap<>();
        
    private BigDecimal money;

    private OrderCouponListGet orderCouponListGet = new OrderCouponListGet(new AsyCallBack<OrderCouponListGet.OrderCouponListEntity>(){
        @Override
        public void onSuccess(String toast, int type, OrderCouponListGet.OrderCouponListEntity orderCouponListEntity) throws Exception {

            OrderCouponListGet.OrderCouponEntity orderCouponEntity = new OrderCouponListGet.OrderCouponEntity();

            orderCouponEntity.id = "";
            orderCouponEntity.title = "";
            orderCouponEntity.money = "";
            orderCouponEntity.total = "";
            orderCouponEntity.starttime = "";
            orderCouponEntity.endtime = "";
//            orderCouponEntity.type = "";

            lists.add(orderCouponEntity);

            lists.addAll(orderCouponListEntity.orderCouponList);

            orderCouponListAdapter.initChoose();

            orderCouponListAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(context, toast, 2000);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order_coupon);

        setBackTrue();

        setTitleName(getResources().getString(R.string.mine_coupon_text));

        money = new BigDecimal("0");

        orderCouponListAdapter = new OrderCouponListAdapter(this, lists);
        order_coupon_lv.setAdapter(orderCouponListAdapter);

        order_coupon_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position != 0) {

                    BigDecimal chooseMoney = new BigDecimal(lists.get(position).money);

                    if (orderCouponListAdapter.isChoose.get(position)) {

                        orderCouponListAdapter.isChoose.put(position, false);

                        money = money.subtract(chooseMoney);

                        Log.e("---不选这张了----", money + "");

                        idsMap.remove(position);


                    } else if ((money.add(chooseMoney)).subtract(new BigDecimal("20")).signum() != 1){

                        orderCouponListAdapter.isChoose.put(position, true);

                        money = money.add(chooseMoney);

                        Log.e("---小于20了----", money + "");

                        idsMap.put(position, position);

                    } else {

                        orderCouponListAdapter.initChoose();

                        orderCouponListAdapter.isChoose.put(position, true);

                        money = chooseMoney;

                        Log.e("---大于20了----", money + "");

                        idsMap.clear();
                        
                        idsMap.put(position, position);

                    }

                    orderCouponListAdapter.notifyDataSetChanged();

                    Log.e("---最后----", money + "");

                } else {


                    JSONArray arr = new JSONArray();

                    Intent intent = new Intent();

                    Bundle bundle = new Bundle();

                    bundle.putString("money", lists.get(position).money);
                    bundle.putString("id", lists.get(position).id);
                    bundle.putString("arr", arr.toString());

//                bundle.putString("type", lists.get(position).type);

                    intent.putExtras(bundle);

                    OrderCouponActivity.this.setResult(4, intent);

                    finish();

                    Log.e("---12313---", arr.toString());

                }



            }
        });

        Bundle bundle = getIntent().getExtras();
        orderCouponListGet.classid = bundle.getString("classid");
        orderCouponListGet.user_id = BaseApplication.BasePreferences.readUserId();
        orderCouponListGet.money = bundle.getString("total_price");

        orderCouponListGet.execute(this);


        order_coupon_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.order_coupon_tv:

                JSONArray arr = new JSONArray();

                if (idsMap.size() > 0) {

                    for (Map.Entry<Integer, Integer> entity : idsMap.entrySet()) {

                        JSONObject object = new JSONObject();

                        lists.get(entity.getValue());

                        try {
                            object.put("id", lists.get(entity.getValue()).id);
                            object.put("money", lists.get(entity.getValue()).money);

                            arr.put(object);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }


                    Intent intent = new Intent();

                    Bundle bundle = new Bundle();

                    bundle.putString("money", money.setScale(0, BigDecimal.ROUND_HALF_UP) + "");
                    bundle.putString("id", "");
                    bundle.putString("arr", arr.toString());
                    bundle.putString("count", idsMap.size() + "");
//                bundle.putString("type", lists.get(position).type);

                    Log.e("---12313---", arr.toString());

                    intent.putExtras(bundle);

                    OrderCouponActivity.this.setResult(4, intent);

                    finish();

                } else {

                    Intent intent = new Intent();

                    Bundle bundle = new Bundle();

                    bundle.putString("money", "");
                    bundle.putString("id", "");
                    bundle.putString("arr", arr.toString());
//                bundle.putString("type", lists.get(position).type);

                    Log.e("---12313---", arr.toString());

                    intent.putExtras(bundle);

                    OrderCouponActivity.this.setResult(4, intent);

                    finish();

                }


                break;

        }
    }


//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (keyCode == event.KEYCODE_BACK) {
//
//            Intent intent = new Intent();
//
//            Bundle bundle = new Bundle();
//
//            bundle.putString("money", "");
//            bundle.putString("id", "");
//            bundle.putString("arr", new JSONArray().toString());
//
//            intent.putExtras(bundle);
//
//            OrderCouponActivity.this.setResult(4, intent);
//
//            finish();
//
//            return false;
//        }
//
//
//        return super.onKeyDown(keyCode, event);
//    }


}
